package GUIMain;

import java.awt.event.ActionListener;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.GridBagLayout;
import java.awt.Dimension;

import java.text.ParseException;

import java.io.IOException;

import javax.mail.internet.AddressException;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import compiler.ContractTypeCheckingReport;
import compiler.TopolMissingDocumentReport;
import compiler.UnscheduleOpenPOReport;
import compiler.WBIMissingDocumentReport;

import data.Reference;
import data.ReportInterface;
import data.ReportTools;
import data.ConfigInfo;

import fileIO.XLSParser;

import mail_operations.AdvancedOptionDialog;
import mail_operations.MailerGUI;

public class OriginReportGUI extends ReportInterface{
	private JDialog dialog;
	private JLabel inputLabel;
	private JTextField inputField;
	private JButton inputButton;
	private JLabel saveLabel;
	private JTextField saveField;
	private JButton saveButton;
	private JCheckBox emailCheckBox;
	private JButton emailButton;
	private JCheckBox reminderCheckBox;
	private JRadioButton allRadioButton;
	private JRadioButton selectedRadioButton;
	private JButton editProfileButton;
	private JButton cancel;
	private JButton run;
	private JButton emailOptionsButton;
	private GridBagConstraints gbc;
	
	public OriginReportGUI(){
		initialize();
		ReportTools.addReport("Late PO Report",this);
	}
	public void initialize(){
		gbc = new GridBagConstraints();
		dialog = new JDialog();
		dialog.setTitle("TOPOL Document Checking Report Generator");
		dialog.setLayout(new GridBagLayout());
		inputLabel = new JLabel("Input File");
		inputField = new JTextField();
		inputButton = new JButton("Select");
		saveLabel = new JLabel("Save Directory");
		saveField = new JTextField();
		saveButton = new JButton("Select");
		emailCheckBox = new JCheckBox("Email");
		emailButton = new JButton("Edit Templates");
		emailOptionsButton = new JButton("Email Options");
		emailOptionsButton.setEnabled(false);
		emailButton.setEnabled(false);
		reminderCheckBox = new JCheckBox("Check 2nd & 3rd Reminder");
		reminderCheckBox.setSelected(false);
		allRadioButton = new JRadioButton("All Profiles");
		selectedRadioButton = new JRadioButton("Selected Profiles");
		editProfileButton = new JButton("Edit Profiles");
		editProfileButton.setEnabled(false);
		cancel = new JButton("Cancel");
		run = new JButton("Run");
		
		gbc.insets.left = 4;
		gbc.insets.right =4;
		gbc.insets.top = 4;
		gbc.insets.bottom = 4;
		
		JPanel fileIO = new JPanel();
		fileIO.setBorder(BorderFactory.createTitledBorder("File IO"));
		fileIO.setLayout(new GridBagLayout());
		
		gbc.anchor = GridBagConstraints.LINE_START;
		gbc.gridx =0;
		gbc.gridy =0;
		fileIO.add(inputLabel,gbc);
		gbc.gridx =0;
		gbc.gridy =1;
		gbc.gridwidth =2;
		inputField.setPreferredSize(new Dimension(300,25));
		fileIO.add(inputField,gbc);
		gbc.gridx =2;
		gbc.gridy =1;
		gbc.gridwidth =1;
		fileIO.add(inputButton,gbc);
		
		gbc.gridx =0;		
		gbc.gridy =2;
		fileIO.add(saveLabel,gbc);
		gbc.gridx =0;
		gbc.gridy =3;
		gbc.gridwidth =2;
		saveField.setPreferredSize(new Dimension(300,25));
		fileIO.add(saveField,gbc);
		gbc.gridx =2;
		gbc.gridy =3;
		gbc.gridwidth =1;
		fileIO.add(saveButton,gbc);
		
		gbc.gridx =0;
		gbc.gridy = 0;
		gbc.gridheight =3;
		fileIO.setPreferredSize(new Dimension(400,250));
		dialog.add(fileIO,gbc);
		
		JPanel optionsPanel = new JPanel();
		optionsPanel.setLayout(new GridBagLayout());
		optionsPanel.setBorder(BorderFactory.createTitledBorder("Options"));
		
		gbc.gridheight =1;
		gbc.gridx = 0;
		gbc.gridy = 0;
		optionsPanel.add(emailCheckBox,gbc);
		gbc.gridx = 1;
		gbc.gridy = 0;
		optionsPanel.add(emailButton,gbc);
		gbc.gridx = 1;
		gbc.gridy = 2;
		optionsPanel.add(emailOptionsButton,gbc);
		gbc.gridx = 0;
		gbc.gridy = 3;
		gbc.gridwidth =2;
		optionsPanel.add(reminderCheckBox,gbc);
		
		JPanel profilePanel = new JPanel();
		profilePanel.setLayout(new GridBagLayout());
		profilePanel.setBorder(BorderFactory.createTitledBorder("Profiles"));
		
		ButtonGroup bg =new ButtonGroup();
		bg.add(allRadioButton);
		bg.add(selectedRadioButton);

		gbc.gridx =0;
		gbc.gridy =0;
		allRadioButton.setSelected(true);
		profilePanel.add(allRadioButton,gbc);
		gbc.gridx =0;
		gbc.gridy =1;
		profilePanel.add(selectedRadioButton,gbc);
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx =0;
		gbc.gridy =2;
		profilePanel.add(editProfileButton,gbc);
		
		gbc.gridwidth =1;
		gbc.gridx =1;
		gbc.gridy =0;
		gbc.gridheight =1;
		optionsPanel.setPreferredSize(new Dimension(200,120));
		dialog.add(optionsPanel,gbc);
		gbc.gridheight =1;
		
		
		gbc.gridx =1;
		gbc.gridy =2;
		gbc.gridwidth = 1;
		profilePanel.setPreferredSize(new Dimension(200,130));
		dialog.add(profilePanel,gbc);
		
		JPanel buttonPanel = new JPanel();
		gbc.gridx =0;
		gbc.gridy =0;
		buttonPanel.add(cancel,gbc);
		gbc.gridx =1;
		gbc.gridy =0;
		buttonPanel.add(run,gbc);
		
		gbc.anchor = GridBagConstraints.CENTER;
		gbc.gridx =0;
		gbc.gridy =3;
		gbc.gridwidth = 2;
		dialog.add(buttonPanel,gbc);
		
		attachListeners();
		
		GUIFocusManager.setDialogProperties(dialog);
	}
	private void attachListeners(){
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				GUIFocusManager.remove();
			}
		});
		emailCheckBox.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(emailCheckBox.isSelected()){
					emailButton.setEnabled(true);
					emailOptionsButton.setEnabled(true);
					reminderCheckBox.setSelected(true);
				}else{
					emailButton.setEnabled(false);
					emailOptionsButton.setEnabled(false);
					reminderCheckBox.setSelected(false);
				}
			}
		});
		emailButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				MailerGUI.open();
			}
		});
		run.addActionListener(new ActionListener(){
			//Check all parameters for errors, then run report generator
			public void actionPerformed(ActionEvent e){
				//Check to ensure the email contents are set before allowing user to mail
				if(emailCheckBox.isSelected()){
					boolean canEmail = ReportTools.checkEmailAbility(false);
					if(!canEmail){
						return;
					}
				}
				
				String inputText = inputField.getText();
				
				if(inputText ==null || inputText.length()<1){
					JOptionPane.showMessageDialog(null, "Please specify an input file");
					inputField.requestFocus();
				}else if(saveField.getText().length()<1){
					JOptionPane.showMessageDialog(null, "Please specify an output directory");
					saveField.requestFocus();
				}else{
					HSSFWorkbook wkb = XLSParser.openXLS(inputField.getText());
					if(wkb==null){
						JOptionPane.showMessageDialog(null, "Input file does not exist or is an invalid type.  Try opening file and resaving as .XLS.");
					}else{
						//Passed initial testing, save as default config 
						ConfigInfo.SAVE_DIRECTORY = saveField.getText();
						//Beginning run, start timer
						GUILoader.getClock().addClock("TOPOL Doc Checking Report");
						//Run report
						try {
							try {
								System.out.println(Reference.reportNo);
								if(Reference.reportNo==1){
									//Missing Doc Report for TOPOL
									TopolMissingDocumentReport.run(wkb, emailCheckBox.isSelected(), reminderCheckBox.isSelected());
								}else if(Reference.reportNo==2){
									//Missing Doc Report for WBI
									WBIMissingDocumentReport.run(wkb, emailCheckBox.isSelected(), reminderCheckBox.isSelected());
								}else if(Reference.reportNo==3){
									//Unscheduled PO Report
									UnscheduleOpenPOReport.run(wkb, emailCheckBox.isSelected(),reminderCheckBox.isSelected());
								}else if(Reference.reportNo==4){
									//Contract Type Checking Report
									ContractTypeCheckingReport.run(wkb, emailCheckBox.isSelected(),reminderCheckBox.isSelected());
								}
							} catch (ParseException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} catch (AddressException e3) {
								// TODO Auto-generated catch block
								e3.printStackTrace();
							}
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						String time = Double.toString(GUILoader.getClock().getClockTime("TOPOL Doc Checking Report"));
						if(emailCheckBox.isSelected()){
							JOptionPane.showMessageDialog(null,"Emails sent successfully - Time elasped: " +time+"ms");
						}else{
							JOptionPane.showMessageDialog(null,"File write successful - Time elasped: " +time+"ms");
						}
						GUIFocusManager.remove();
					}
				}
			}
		});
		emailOptionsButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				AdvancedOptionDialog.open();
			}
		});
		inputButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				FileChooser.openFileChooser(dialog, false);
				if(FileChooser.getSelectedFile()!=null){
					inputField.setText(FileChooser.getSelectedFile().getAbsolutePath());
				}
			}
		});
		saveButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				FileChooser.openFileChooser(dialog, true);
				if(FileChooser.getSelectedFile()!=null){
					saveField.setText(FileChooser.getSelectedFile().getAbsolutePath());
				}
			}
		});
		editProfileButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				ProfileEditorGUI.open();
			}
		});
		ActionListener radioListener = new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(selectedRadioButton.isSelected()){
					editProfileButton.setEnabled(true);
				}else{
					editProfileButton.setEnabled(false);
				}
			}
		};
		selectedRadioButton.addActionListener(radioListener);
		allRadioButton.addActionListener(radioListener);
	}
	public void open() {
		saveField.setText(ConfigInfo.SAVE_DIRECTORY);
		inputField.setText(ConfigInfo.INPUT_DIRECTORY);
		GUIFocusManager.add(dialog);
	}
}
